import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../tabber.dart';
import 'auth_vm.dart';
import 'common.dart';

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => AuthViewModel(),
      child: Scaffold(
        backgroundColor: Colors.teal,
        body: Container(
          padding: EdgeInsets.all(20.0),
          child: Consumer<AuthViewModel>(
            builder: (context, vm, child) {
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  connonInput(labelText: '输入账号', onChanged: (value) => vm.loginInfo.username = value),
                  SizedBox(height: 16.0),
                  connonInput(labelText: '输入密码', onChanged: (value) => vm.loginInfo.password = value),
                  SizedBox(height: 50.0),
                  commonButton(text: '登录', border: true, onPressed: () async {
                    bool? success = await vm.login();
                    if (success == true) {
                      Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => Tabber()), (route) => false);
                    }
                  }),
                  SizedBox(height: 5.0),
                  commonButton(text: '注册账号', onPressed: () {
                    Navigator.pushNamed(context, '/register');
                  }),
                ],
              );
            },
          ),
        )
      )
    );

  }

  
}